<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" class="demo-form-inline filter-container">
      <el-form-item prop="dateType" label="时间：">
        <el-date-picker
          v-model="form.time"
          class="filter-item"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd"
          align="right"
        ></el-date-picker>
      </el-form-item>
      <el-form-item v-if="roleCheck('virtual')" class="with-pre-select" label="数据类型">
        <el-select v-model="form.isFake">
          <el-option v-for="item in consts.STATISTICS_RANGE"
                     :key="item.value"
                     :label="item.title"
                     :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" v-waves icon="el-icon-search" @click="submitFilterForm">搜索</el-button>
      <el-button class="filter-item" type="primary" icon="el-icon-download" @click="exportExcel">导出</el-button>
    </el-form>
    <div class="tip">
      <el-tip>
        注：目前只支持平层搜索，不可跨层搜索
      </el-tip>
    </div>
    <el-table
      :data="tableData"
      v-loading="loading"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      show-summary
      :summary-method="getLocalSummaries"
      style="width: 100%"
      :header-cell-class-name="setHeaderStyle"
      :cell-class-name="setCellStyle"
    >
      <el-table-column label="日期" align="center" fixed="left" width="140px">
        <template slot-scope="{row}">
          {{row.date}}
        </template>
      </el-table-column>
      <el-table-column label="查看详情" align="center" fixed="left" width="120px">
        <template slot-scope="{row}">
          <!--新版本这里跳转数据sublist.vue需要修改-->
          <el-button type="primary" plain
                     class="pointer"
                     @click.native="openDialog({props:row, dialogName: 'custom-dialog'})"
          >查看详情
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="注册数" prop="registerNum" align="center" sortable></el-table-column>
      <el-table-column label="首存数" prop="firstRechargeNum" align="center" sortable></el-table-column>
      <el-table-column label="首存金额" prop="firstRechargeSum" align="center" sortable>
        <template slot-scope="{row}">{{row.firstRechargeSum | currency}}</template>
      </el-table-column>
      <el-table-column label="存款人数" align="center" prop="rechargeNum" sortable></el-table-column>
      <el-table-column label="存款次数" align="center" prop="rechargeTimes" sortable></el-table-column>
      <el-table-column label="取款人数" align="center" prop="withdrawNum" sortable></el-table-column>
      <el-table-column label="取款次数" align="center" prop="withdrawTimes" sortable></el-table-column>
      <el-table-column label="存款金额" align="center" width="150" prop="rechargeSum" sortable>
        <template slot-scope="{row}">{{row.rechargeSum | currency}}</template>
      </el-table-column>
      <el-table-column label="存款手续费" align="center" width="150" prop="rechargeFeeSum" sortable>
        <template slot-scope="{row}">{{row.rechargeFeeSum | currency}}</template>
      </el-table-column>
      <el-table-column label="取款金额" align="center" width="150" prop="withdrawSum" sortable>
        <template slot-scope="{row}">{{row.withdrawSum | currency}}</template>
      </el-table-column>
      <el-table-column label="取款手续费" align="center" width="150" prop="withdrawFeeSum" sortable>
        <template slot-scope="{row}">{{row.withdrawFeeSum | currency}}</template>
      </el-table-column>
      <!--盈棋牌亏重新计算后的字段-->
      <el-table-column label="公司" align="center">
        <el-table-column label="有效投注额" align="center" width="130" prop="betEffectSum" sortable>
          <template slot-scope="{row}">{{row.betEffectSum | currency}}</template>
        </el-table-column>
        <el-table-column label="总投注额" align="center" width="130" prop="betSum" sortable>
          <template slot-scope="{row}">{{row.betSum | currency}}</template>
        </el-table-column>

        <el-table-column label="派彩额" align="center" width="130" prop="prizeSum" sortable>
          <template slot-scope="{row}">{{row.prizeSum | currency}}</template>
        </el-table-column>
        <el-table-column label="公司输赢" align="center" width="130" prop="winSum" sortable>
          <template slot-scope="{row}">
            <span v-if="row.winSum<0" style="color: #00f700;">{{ row.winSum | currency}}</span>
            <span v-else style="color: red">{{ row.winSum | currency}}</span>
          </template>
        </el-table-column>
      </el-table-column>

      <el-table-column label="玩家对战类" align="center">
        <el-table-column label="总投注额" align="center" width="130" prop="playerBetSum" sortable>
          <template slot-scope="{row}">{{row.playerBetSum | currency}}</template>
        </el-table-column>
        <el-table-column label="输赢" align="center" width="130" prop="playerWinSum" sortable>
          <template slot-scope="{row}">{{row.playerWinSum | currency}}</template>
        </el-table-column>
        <el-table-column label="公司抽水" align="center" width="130" prop="playerWaterFee" sortable>
          <template slot-scope="{row}">{{row.playerWaterFee | currency}}</template>
        </el-table-column>
      </el-table-column>
      <el-table-column label="公司总盈利" align="center" width="130" prop="totalWin" sortable>
        <template slot-scope="{row}">{{row.totalWin | currency}}</template>
      </el-table-column>
      <!--结束-->
      <!--新版本不需要的-->
      <!-- <el-table-column label="总投注额" align="center" width="150" prop="bettingTotalSum" sortable>
        <template slot-scope="{row}">{{row.bettingTotalSum | currency}}</template>
      </el-table-column>
      <el-table-column label="有效投注额" align="center" width="150" prop="bettingSum" sortable>
        <template slot-scope="{row}">{{row.bettingSum | currency}}</template>
      </el-table-column>
      <el-table-column label="派彩额" align="center" width="150" prop="prizeSum" sortable>
        <template slot-scope="{row}">{{row.prizeSum | currency}}</template>
      </el-table-column>
      <el-table-column label="公司输赢" align="center" width="150" prop="winSum">
        <template slot-scope="{row}">
          <span v-if="row.winSum<0" style="color: #00f700;">{{-1 * row.winSum | currency}}</span>
          <span v-else style="color: red">{{-1 * row.winSum | currency}}</span>
        </template>
      </el-table-column> -->

      <el-table-column label="红利（包括加币-计入红利）" align="center" width="150" prop="bonusSum">
        <template slot-scope="{row}">{{row.bonusSum | currency}}</template>
      </el-table-column>
      <el-table-column label="加币（不计红利）" align="center" width="150" prop="addCoinSum">
        <template slot-scope="{row}">{{row.addCoinSum | currency}}</template>
      </el-table-column>
      <el-table-column label="减币" align="center" width="150" prop="subCoinSum">
        <template slot-scope="{row}">{{row.subCoinSum | currency}}</template>
      </el-table-column>

      <el-table-column label="返水" align="center" width="150" prop="rebateSum">
        <template slot-scope="{row}">{{row.rebateSum | currency}}</template>
      </el-table-column>
    </el-table>
    <custom-dialog :visible="dialogVisible" returnButtonName="返回" :callback="closeDialog">
      <div slot="title">&nbsp;</div>
      <sub-list v-if="dialogVisible" :row-data="dialogProps"></sub-list>
    </custom-dialog>
  </div>
</template>

<script>
import { form, grid, dialog } from '@/build';

import SubList from './subList';

import { reportCompanyListApi as listApi } from '@/services/api/report';

export default {
  name: 'report-company',

  mixins: [form, grid, dialog],

  components: {
    SubList,
  },

  data() {
    const monthTime = [
      moment()
        .startOf('month')
        .format('YYYY-MM-DD'),
      moment()
        .format('YYYY-MM-DD'),
    ];
    return {
      tableData: [],
      form: {
        time: monthTime,
        userName: undefined,
        userType: -1,
        isFake: 0,
      },
      filters: {
        stime: moment(monthTime[0])
          .valueOf(),
        etime: moment(monthTime[1])
          .valueOf() + 86399999,
        size: 20,
        page: 1,
        parentId: 1,
        export: undefined,
        isFake: 0,
      },
      filterDateType: false,
      statistic: [
        {
          index: 4,
          prop: 'firstRechargeNum',
          format: value => _.currency(value),
        },
        {
          index: 9,
          prop: 'rechargeSum',
          format: value => _.currency(value),
        },
        {
          index: 10,
          prop: 'rechargeFeeSum',
          format: value => _.currency(value),
        },
        {
          index: 11,
          prop: 'withdrawSum',
          format: value => _.currency(value),
        },
        {
          index: 12,
          prop: 'withdrawFeeSum',
          format: value => _.currency(value),
        },
        {
          index: 13,
          prop: 'betSum',
          format: value => _.currency(value),
        },

        {
          index: 14,
          prop: 'betEffectSum',
          format: value => _.currency(value),
        },
        {
          index: 15,
          prop: 'prizeSum',
          format: value => _.currency(value),
        },
        {
          index: 16,
          prop: 'winSum',
          format: value => _.currency(value),
        },
        {
          index: 17,
          prop: 'playerBetSum',
          format: value => _.currency(value),
        },
        {
          index: 18,
          prop: 'playerWinSum',
          format: value => _.currency(value),
        },
        {
          index: 19,
          prop: 'playerWaterFee',
          format: value => _.currency(value),
        },
        {
          index: 20,
          prop: 'totalWin',
          format: value => _.currency(value),
        },
        {
          index: 21,
          prop: 'bonusSum',
          format: value => _.currency(value),
        },
        {
          index: 22,
          prop: 'addCoinSum',
          format: value => _.currency(value),
        },
        {
          index: 23,
          prop: 'subCoinSum',
          format: value => _.currency(value),
        },
        {
          index: 24,
          prop: 'rebateSum',
          format: value => _.currency(value),
        },
      ],
    };
  },
  methods: {
    setHeaderStyle({
      row, column, rowIndex, columnIndex,
    }) {
      if ([13].includes(columnIndex) && rowIndex == 0) {
        return 'header-green';
      }
      if ([0, 1, 2, 3].includes(columnIndex) && rowIndex == 1) {
        return 'header-green';
      }
      if ([14].includes(columnIndex) && rowIndex == 0) {
        return 'header-yellow';
      }
      if ([4, 5, 6].includes(columnIndex) && rowIndex == 1) {
        return 'header-yellow';
      }
      if ([13, 15].includes(columnIndex)) {
        return 'border-left';
      }
    },
    setCellStyle({
      row, column, rowIndex, columnIndex,
    }) {
      if ([13, 14, 15, 16].includes(columnIndex)) {
        return 'header-green';
      }
      if ([17, 18, 19].includes(columnIndex)) {
        return 'header-yellow';
      }
      if ([20].includes(columnIndex)) {
        return 'border-left';
      }
    },
    submitFilterForm() {
      const { time } = this.form;
      let stime;
      let etime;
      if (!_.isUndefined(time) && _.isArray(time)) {
        stime = moment(time[0])
          .valueOf();
        etime = moment(time[1])
          .valueOf() + 86399999;
      }

      this.filters = {
        ...this.filters,
        ...this.form,
        stime,
        etime,
      };
      this.getAll();
    },
  },

  mounted() {
    this.bindGet(listApi, 'tableData', {
      sendingData: 'filters',
      action: 'getGrid',
      format: (data) => {
        const result = [];
        let sortData = [];
        _.forIn(data, (value, key) => {
          result.push({
            ...value,
            date: key,
          });
        });
        sortData = _.sortBy(result, item => -new Date(item.date).getTime());
        return sortData;
      },
    });

    this.getAll();
  },
};
</script>

<style>
  .header-green{background: rgb(235,235,255) !important;color:#333;border:1px solid #aaa;}
  .header-yellow{background:  rgb(255,235,235) !important;color:#333;border:1px solid #aaa;}
  .border-left{border-left:1px solid #aaa;}
</style>
